package org.ccay.excel.exporter.internal.impl;


import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.async.AsyncMessage;
import org.ccay.async.IMessageProcessor;
import org.ccay.async.task.service.IExportTaskService;
import org.ccay.core.exception.ApplicationException;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;
import org.ccay.core.util.exception.NoDataFoundException;
import org.ccay.excel.exception.ExcelExportException;
import org.ccay.excel.exporter.ExportTaskRequest;
import org.ccay.excel.exporter.service.impl.internal.ExcelExportContext;
import org.ccay.excel.exporter.service.impl.internal.ExcelStreamWriter;
import org.ccay.excel.exporter.service.impl.internal.ExportMonitor;
//import org.ccay.mail.service.IMailService;
import org.ccay.message.service.IPushMessageService;

/**
 * Export 异步消息处理器	
 * @author chaowangbang
 * @since 2012-5-25
 */
@Named
public class ExcelExportProcessor implements IMessageProcessor {

	private static final ILogger log = CcayLoggerFactory.getLogger(ExcelExportProcessor.class);
	
	@Inject
	private IExportTaskService exportTaskService;
	
//	/**
//	 * 异步的邮件服务
//	 */
//	@Inject
//	@Named("defaultMailService")
//	private IMailService defaultMailService;
	
	/** 消息推送服务 **/
	@Inject
	private IPushMessageService pushMessageService;
	/* (non-Javadoc)
	 * @see org.ccay.async.IMessageProcessor#process(org.ccay.async.AsyncMessage)
	 */
	public void process(AsyncMessage message) throws ApplicationException {
		ExportTaskRequest taskRequest = (ExportTaskRequest) message.getContent();
		/**
		 * 最多等待120秒
		 */
		int waitTime = 120000;
		//只有当 等待时间在120秒范围内  循序查询 超过120秒 不处理
		while (waitTime>0 ) {
			try {
				Thread.sleep(5000);
				exportTaskService.findExportTaskById(Integer.valueOf(taskRequest.getTaskId()));
				//如果可以没异常执行完成  那么说明有任务处理  跳出循环
				break;
			} catch (InterruptedException e) {
				log.error("Thread sleep error."+e);
			}catch (NoDataFoundException e1) {
				log.error("ExportTask NoDataFound"+e1);
			}
			//没有跳出 说明那边事务还没提交  再继续等待
			waitTime -= 5000;
		}
		try {
			exportExcel(taskRequest);
		} catch (ApplicationException ex) {
			log.error("The exception occured during exportting excel."+ex);
		}
		
	}
	private void exportExcel(ExportTaskRequest taskRequest)throws ApplicationException{
		ExportMonitor monitor = new ExportMonitor();
		monitor.setExportTaskService(exportTaskService);
//		monitor.setDefaultMailService(defaultMailService);
		monitor.setPushMessageService(pushMessageService);
		monitor.setExportRequest(taskRequest);
		monitor.begin();
		
		ExcelExportContext context=new ExcelExportContext(taskRequest);
		ExcelStreamWriter writer=new ExcelStreamWriter(context);
		
		try{
			//导出过程实现
			writer.writer();
			monitor.end();
		}catch(ExcelExportException eex){
			//处理异常
			log.error(eex);
			monitor.fail(eex);
		}catch(RuntimeException rex){
			//处理异常
			log.error(rex);
			monitor.fail(rex);
		}catch(Exception ex){
			//处理异常
			log.error(ex);
			monitor.fail(ex);
		}catch(Error er){
			//处理异常
			log.error(er);
			monitor.fail(er);
			throw er;
		}finally{
			if(null != writer){
				writer.clearObject();
			}
		}
	}

}
